package com.j1cn.medical.manage.webservice.controller;

import com.google.common.collect.Lists;
import com.j1cn.medical.manage.biz.UserService;
import com.j1cn.medical.manage.dao.entity.SysResource;
import com.j1cn.medical.manage.dao.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import static java.net.http.HttpRequest.BodyPublishers;
import static java.net.http.HttpResponse.BodyHandlers;

/**
 * @author ：ernest.
 * @date ：Created in 2019/03/28 16:39
 * @description：
 * @modified by：
 */
@RestController
@RequestMapping("/user")
@Api("UserController")
public class UserController {
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserService userService;

    @PostMapping("/userinfo")
    @ApiOperation(value="获取用户信息", notes="获取认证通过的用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "userName", value = "用户名称", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "password", value = "密码", required = true, dataType = "String")
    })
    public DataJsonResult getSysUserInfo(String userName, String password, Principal principal, Authentication authentication){
        var dataJsonResult = new DataJsonResult();
        if (principal.getName().equals(userName)){
            Optional<SysUser> user = userService.getByUserName(userName);
            SysUser sysUser = user.get();

            ArrayList<String> roles = new ArrayList<>();
            roles.add("admin");

            sysUser.setRoles(roles);
            if (passwordEncoder.matches(password, sysUser.getPassword())){
                dataJsonResult.setCode("1");
                dataJsonResult.setMsg("ssdsssssssss");
                dataJsonResult.setData(sysUser);
            }

        }
        return dataJsonResult;
    }


    public static void main(String[] args) throws IOException, InterruptedException {
        var request = HttpRequest.newBuilder()
                .uri(URI.create("https://www.baidu.com"))
                .POST(BodyPublishers.ofString("{}"))
                .build();
        var httpClient = HttpClient.newHttpClient();
        var response = httpClient.send(request, BodyHandlers.ofString());
        System.out.println(response.headers());
    }


}
